<!-- 建档表单 -->
<template>
	<view class="filing">

		<form @submit="formSubmit">
			<view class="uni-common-mt">
				<view class="uni-form-item uni-column user-input">
					<view class="title">姓名</view>
					<view class="input">
						<input class="uni-input" name="Name" v-model="formData.Name" maxlength="10"
							placeholder="请输入姓名" />
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">性别</view>
					<view class="input">
						<radio-group name="Sex" @change="bingSexChange">
							<label>
								<radio value="1" /><text>男</text>
							</label>
							<text style="margin-right: 10px;"></text>
							<label>
								<radio value="2" /><text>女</text>
							</label>
						</radio-group>
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">出生日期</view>
					<view class="input">
						<picker mode="date" :value="date" @change="bindDateChange">
							<view class="uni-input">{{date || '请选择'}}</view>
						</picker>
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">身份证号</view>
					<view class="input">
						<input class="uni-input" type="idcard" name="IDNum" v-model="formData.IDNum" maxlength="18"
							placeholder="请输入身份证号" />
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">手机号</view>
					<view class="input">
						<input class="uni-input" v-model="formData.PhoneNo" maxlength="50" disabled
							placeholder="请授权获取,无法手动输入" />
						<button style="margin-top: 10px;" class="mini-btn" type="primary" size="mini"
							open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权获取手机号</button>
						<!-- <text>{{formData.PhoneNo ? formData.PhoneNo:'系统自动获取'}}</text> -->
					</view>
				</view>
				<view class="fenjie"></view>
				<!-- <view class="uni-form-item uni-column user-input">
					<view class="title">交易密码</view>
					<view class="input">
						<input class="uni-input" password v-model="formData.Password" maxlength="30"
							placeholder="方便您收货的地址" />
					</view>
				</view> -->
			</view>
			<button style="margin-top: 10px;" type="default" form-type="submit">提交信息</button>
		</form>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex'
	import graceChecker from "../../../common/graceChecker.js"
	import WXBizDataCrypt from "../../../common/WXBizDataCrypt.js"

	function getDate(type) {
		const date = new Date();

		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();

		if (type === 'start') {
			year = year - 10;
		} else if (type === 'end') {
			year = year + 10;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;

		return `${year}-${month}-${day}`;
	}

	export default {
		data() {
			return {
				openid: '',
				formData: {
					"CardNo": "",
					"CardType": "1",
					"Name": "",
					"Sex": "1",
					"BirthDay": "",
					"IDNum": "",
					"IDType": '01',
					"PhoneNo": "",
					"Password": ""
				},

				date: '',
				startDate: getDate('start'),
				endDate: getDate('end'),
			}
		},
		computed: {
			...mapState(['baseUrl'])
		},
		methods: {
			...mapMutations(['setCardId', 'setHealthId']),
			getPhoneNumber(e) {
				console.log(e)
				let openid = uni.getStorageSync('openid')
				let session_key = uni.getStorageSync('session_key')
				if (e.detail && e.detail.errMsg === 'getPhoneNumber:ok') {
					let pc = new WXBizDataCrypt('wxeda90b40cd05317c', session_key);
					let data = pc.decryptData(e.detail.encryptedData, e.detail.iv);
					if (data.phoneNumber) {
						this.formData.PhoneNo = data.phoneNumber
					}
				}
			},
			formSubmit: function(e) {
				let that = this
				this.formData.BirthDay = this.date
				
				// console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
				//定义表单规则
				var rule = [
				];
				
				if (!this.formData.Name) {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none'
					});
					return
				}
				if (!this.formData.Sex) {
					uni.showToast({
						title: '请输入性别',
						icon: 'none'
					});
					return
				}
				if (!this.formData.BirthDay) {
					uni.showToast({
						title: '请输入出生日期',
						icon: 'none'
					});
					return
				}
				if (!this.formData.IDNum) {
					uni.showToast({
						title: '请输入身份证号',
						icon: 'none'
					});
					return
				}
				if (this.formData.IDNum.length != 18) {
					uni.showToast({
						title: '您输入的身份证号码不合法',
						icon: 'none'
					});
					return
				}
				if (!this.formData.PhoneNo) {
					uni.showToast({
						title: '请授权获取手机号',
						icon: 'none'
					});
					return
				}
				
				//进行表单检查
				var formData = e.detail.value;
				var checkRes = graceChecker.check(formData, rule);
				if (checkRes) {
					// uni.showToast({title:"验证通过!", icon:"none"});
					console.log(this.formData)
					let user_token = uni.getStorageSync('user_token')

					this.$app._post_form2('entry/wxapp/apipatient.createprofile', {
						openid: user_token.openid,
						form: this.formData
					}, function(res) {
						if (res.status == 1) {
							/* let userCardInfo = uni.getStorageSync('user_cardinfo');
							userCardInfo.cardName = res.data.name
							userCardInfo.cardid = res.data.id
							uni.setStorageSync('user_cardinfo', userCardInfo) */
							let openid = uni.getStorageSync('openid')
							let user_cardlist = uni.getStorageSync('user_cardlist')
							let current_user_card = uni.getStorageSync('current_user_card')
							
							that.$app._post_form("entry/wxapp/apipatient.cardlist", {
								'openid': openid
							}, (res) => {
								if (res.status === 1) {
									let userCardList = res.data
									uni.setStorageSync('user_cardlist', userCardList)
									uni.setStorageSync('current_user_card', userCardList[0])
									uni.navigateTo({
										url: '/pages/global/successMessage?msg=建档成功'
									})
								}
							})
						}else {
							uni.showToast({
								title: '建档失败',
								icon: "none"
							});
						}
					})
				} else {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
				}
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
			},
			bingSexChange: function(e) {
				this.formData.Sex = e.detail.value
			}
		}
	}
</script>

<style lang="scss">
	.filing {
		padding: 15px;
	}

	.user-input {
		width: 335px;
		margin: 20px auto;
		display: flex;

		.title {
			width: 70px;
		}

		.input {
			width: 280px;
			margin-left: 20px;
		}
	}

	.fenjie {
		height: 1px;
		background: #ddd;
		margin: 0 auto;
	}

	.up-button {
		width: 100%;
		height: 40px;
		background: #0096FB;
		border-radius: 4px;
		font-size: 16px;
		line-height: 40px;
		color: white;
		margin-top: 30px;
	}
</style>